import 'package:flutter/material.dart';
import 'package:flutter_2/pages/CameraPage/CameraMain.dart';
import 'package:flutter_2/pages/SameCity/SameCityPage.dart';

import '../pages/SelfHome/SelfHomePage.dart';

// class BtmBar extends StatelessWidget {
//   const BtmBar({Key? key}) : super(key: key);

//   @override
//   Widget build(BuildContext context) {
//     return Container(
//       child: Row(mainAxisAlignment: MainAxisAlignment.spaceAround, children: [
//         getBtmTextWidget("首页", true),
//         getBtmTextWidget("同城", false),
//         AddIcon(),
//         getBtmTextWidget("消息", false),
//         getBtmTextWidget("我", false),
//       ]),
//     );
//   }
// }

class BtmBar extends StatefulWidget {
  const BtmBar({Key? key, required this.selectIndex}) : super(key: key);
  final int selectIndex;

  @override
  State<BtmBar> createState() => _BtmBarState();
}

class _BtmBarState extends State<BtmBar> {
  List<bool> selected = <bool>[];

  @override
  void initState() {
    // TODO: implement initState
    for (var i = 0; i < 4; i++) {
      selected.add(false);
    }
    selected[widget.selectIndex] = false;
  }

  tapItem(index) {
    selected = <bool>[];
    for (var i = 0; i < 4; i++) {
      selected.add(false);
    }
    selected[index] = true;
    setState(() {
      selected = selected;
    });

    switch (index) {
      case 1:
        Navigator.push(
            context,
            MaterialPageRoute(
                builder: (context) => SameCityPage(
                      selIndex: 1,
                    )));
        break;
      case 2:
        Navigator.push(
            context, MaterialPageRoute(builder: (context) => CameraPage()));
        break;
      case 4:
        Navigator.push(
            context, MaterialPageRoute(builder: (context) => SelfHomePage()));
        break;
      default:
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Row(mainAxisAlignment: MainAxisAlignment.spaceAround, children: [
        getBtmTextWidget("首页", context, selected[0], () {
          tapItem(0);
        }),
        getBtmTextWidget("同城", context, selected[1], () {
          tapItem(1);
        }),
        AddIcon(
          tapItem: () {
            tapItem(2);
          },
        ),
        getBtmTextWidget("消息", context, selected[2], () {
          tapItem(3);
        }),
        getBtmTextWidget("我", context, selected[3], () {
          tapItem(4);
        }),
      ]),
    );
  }
}

getBtmTextWidget(
    String content, BuildContext context, bool isSelected, tapFun) {
  double rpx = MediaQuery.of(context).size.width / 750;
  return Container(
    width: 150 * rpx,
    child: FlatButton(
      onPressed: () {
        tapFun();
      },
      child: Text(
        "$content",
        style: isSelected
            ? TextStyle(
                fontSize: 18, color: Colors.white, fontWeight: FontWeight.bold)
            : TextStyle(fontSize: 18, color: Colors.grey[600]),
      ),
    ),
  );
}

//+
class AddIcon extends StatelessWidget {
  const AddIcon({Key? key, required this.tapItem}) : super(key: key);
  final VoidCallback tapItem;
  @override
  Widget build(BuildContext context) {
    return Container(
      // decoration: BoxDecoration(borderRadius: BorderRadius.circular(5)),
      height: 35,
      width: 60,
      child: FlatButton(
        padding: EdgeInsets.all(0),
        onPressed: () {
          tapItem();
        },
        child: Stack(
          children: [
            Positioned(
              height: 35,
              width: 50,
              child: Container(
                decoration: BoxDecoration(
                    color: Colors.cyan,
                    borderRadius: BorderRadius.circular(10)),
              ),
            ),
            Positioned(
              height: 35,
              width: 50,
              right: 0,
              child: Container(
                decoration: BoxDecoration(
                    color: Colors.redAccent,
                    borderRadius: BorderRadius.circular(10)),
              ),
            ),
            Positioned(
              height: 35,
              width: 50,
              right: 5,
              child: Container(
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(10)),
                child: Icon(Icons.add),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
